﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
using JBORPG;
using JBORPG.Net;
using System.IO;

namespace JBORPG.Client
{
    public enum MessageType
    {
        Unknown = 0,
        Normal,
        Tell,
        System,
        Combat        
    }

    public partial class Main : Form
    {
        private Net.Client m_Client;
        private Net.Classes.Player m_Player;    
        private IPAddress m_IPAddress;
        private short m_Port;
        private static Main m_Form;

        public Net.Client Client
        {
            get { return m_Client; }
            set { m_Client = value; }
        }

        public static Main MainForm
        {
            get { return Main.m_Form; }
        }

        public Net.Classes.Player ClientPlayer
        {
            get { return m_Player; }
        }

        public Main()
        {
            this.m_IPAddress = IPAddress.Parse(Settings.Default.IPAddress);
            this.m_Port = Settings.Default.Port;

            m_Form = this;
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            Client = new JBORPG.Net.Client(new NetHandler(), new IPEndPoint(m_IPAddress, m_Port));

            using (LoginForm loginForm = new LoginForm(this))
            {
                if (loginForm.ShowDialog() == DialogResult.OK)
                {
                    m_Player = loginForm.Player;
                }
                else
                {
                    this.Close();
                }
            }
        }

        public void AddMessage(String msg, MessageType type)
        {
            AddMessage(msg, type, true);
        }

        public void AddMessage(String msg, MessageType type, Boolean showTime)
        {
            StringBuilder Output = new StringBuilder();

            if (showTime)
            {
                String hour = DateTime.Now.Hour.ToString();
                String min = DateTime.Now.Minute.ToString();

                if (hour.Length <= 1)
                    hour = "0" + hour;
                if (min.Length <= 1)
                    min = "0" + min;

                Output.Append(hour + ":" + min + " ");
            }

            Output.Append(msg);

            int length = MessageFrame.TextLength;
            MessageFrame.AppendText(Output.ToString() + Environment.NewLine);
            MessageFrame.SelectionStart = length;
            MessageFrame.SelectionLength = Output.Length;
            MessageFrame.SelectionColor = GetColorFromType(type);
        }

        private Color GetColorFromType(MessageType type)
        {
            switch (type)
            {
                case MessageType.Normal: return Color.White;
                case MessageType.Tell: return Color.HotPink;
                case MessageType.Combat: return Color.OrangeRed;
                case MessageType.System: return Color.Yellow;
                case MessageType.Unknown: return Color.Turquoise;
            }

            return Color.White;
        }

        private void InputBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (Char)13)
            {
                String msg = InputBox.Text.Trim();
                String method = msg.Substring(0, msg.IndexOf(' '));
                msg = msg.Remove(0, msg.IndexOf(' ') + 1);
                String[] msgParams = msg.Split(' ');

                try
                {
                    Invoker i = m_Client.Invoke("", method, msgParams);
                }
                catch (JBORPGException ex)
                {
                    MessageBox.Show(ex.Message);
                }

                InputBox.Clear();
                e.Handled = true;
            }
        }
    }
}
